﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Routing;

namespace Portal.Web
{
    public class BaseFrontController : BaseController
    {
        private static readonly Type CurrentFrontBaseViewDataKey = typeof(FrontBaseViewData);
        private static readonly Type CurrentSettingKey = typeof(ISetting);
        private static readonly Type CurrentColumnKey = typeof(Guid);
        private static readonly Type CurrentLinkKey = typeof(ILink);
        private ISetting _setting;
        /// <summary>
        /// 设置
        /// </summary>
        public ISetting Settings
        {
            get
            {
                if (_setting != null)
                    return _setting;
                var setting = HttpContext.Items[CurrentSettingKey] as ISetting;
                if (setting == null)
                {
                    setting = SettingCache.GetList().FirstOrDefault(o => o.Culture == Culture);
                    if (setting == null)
                        throw new ArgumentNullException("数据不存在设置记录,{0}".FormatWith("Setting"));
                    HttpContext.Items[CurrentSettingKey] = setting;
                }
                _setting = setting;
                return setting;
            }
        }
        private IList<ILink> _list;
        public IList<ILink> Link
        {
            get
            {
                if (_list != null)
                    return _list;
                var list = HttpContext.Items[CurrentLinkKey] as IList<ILink>;
                if (list == null)
                {
                    list = LinkCache.GetList();
                    HttpContext.Items[CurrentLinkKey] = list;
                }
                _list = list;
                return list;
            }
        }
        /// <summary>
        /// 栏目
        /// </summary>
        public string Column
        {
            get
            {

                var column = HttpContext.Items[CurrentColumnKey];
                if (column == null)
                {
                    HttpContextBase contextWrapper = new HttpContextWrapper(System.Web.HttpContext.Current);
                    RouteData routeData = RouteTable.Routes.GetRouteData(contextWrapper);
                    if (routeData != null)
                    {
                        routeData.Values.TryGetValue("column", out column);

                    }
                }
                return column != null ? column.ToString() : "home_index";
            }
        }

        /// <summary>
        /// 创建一个返回实体
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public virtual T CreateFrontViewData<T>() where T : FrontBaseViewData, new()
        {
            var viewData = HttpContext.Items[CurrentFrontBaseViewDataKey] as T;
            if (viewData == null)
            {
                viewData = new T
                               {
                                   Mobile = Settings.Mobile,
                                   Telnet = Settings.Telnet,
                                   CompanyAdress = Settings.CompanyAdress,
                                   Fax = Settings.Fax,
                                   Email = Settings.Email,
                                   MetaDescription = Settings.MetaDescription,
                                   MetaKeywords = Settings.MetaKeywords,
                                   About = Settings.About,
                                   AccessCountHtml = Settings.AccessCountHtml,
                                   ZipCode = Settings.Code,
                                   Contacter = Settings.Contacter,
                                   Title = Settings.Title,
                                   Qq = Settings.Qq,
                                   Recruitment = Settings.Recruitment,
                                   CompanyName = Settings.CompanyName,
                                   SalesNetwork = Settings.SalesNetwork,
                                   NationalHotLine = Settings.HotLineTelnet,
                                   Copyright = Settings.Copyright,
                                   ContactUs = Settings.ContactUs,
                                   Culture = Settings.Culture,
                                   CurrentUrl = Request.Url != null ? Request.Url.ToString() : "",
                                   Video = Settings.Video,
                                   Link = Link,
                                   Url = Request.Url != null ? Request.Url.ToString() : "",
                                   OrderCompanyName = Settings.OrderCompanyName,
                                   OrderEmail = Settings.OrderEmail,
                                   CompanyProfile = Settings.CompanyProfile,
                                   ProductCategory = Category.Where(o => o.Type == (int)CategoryTypeEnum.Product).OrderBy(o => o.Sort).ToList()
                               };
                HttpContext.Items[CurrentFrontBaseViewDataKey] = viewData;
            }
            return viewData;

        }

    }
}
